<?php
namespace app\payclass\model;

use app\BaseModel;
use think\facade\Db;

class LookRecord extends BaseModel
{


    //定义表名
    protected $name = 'longbing_card_payclass_look_record';


    /**
     * @author chenniang
     * @DataTime: 2020-12-08 10:51
     * @功能说明:列表
     */
    public function recordList($dis){


      $data = $this->alias('a')
              ->join('longbing_card_payclass_list b','a.class_id = b.id')
              ->where($dis)
              ->field('a.user_id,MAX(a.create_time) as create_time,a.class_id,b.cover,a.id,b.title')
              ->group('a.class_id')
              ->order('a.create_time desc')
              ->limit(10)
              ->select()
              ->toArray();

      if(!empty($data)){

          foreach ($data as $k=>$v){

              $where = [

                  'user_id' => $v['user_id'],

                  'class_id'=> $v['class_id']
              ];

              $data[$k]['video_id'] = $this->where($where)->order('create_time desc')->value('video_id');
          }
      }

      return $data;
    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-09 17:05
     * @功能说明:配置详情
     */
    public function recordInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)? $data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:配置添加
     */
    public function recordAdd($data){

        $data['create_time'] = time();

        $data['update_time'] = time();

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:配置编辑
     */
    public function recordUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-16 17:18
     * @功能说明:视频观看条数
     */
    public function lookCount($user_id,$class_id){

        $where = [

            'a.user_id' => $user_id,

            'a.class_id'=> $class_id,

            'b.status'  => 1
        ];


        $num = $this->alias('a')
                ->join('longbing_card_payclass_video b','a.video_id = b.id')
                ->where($where)
                ->group('b.id')
                ->count();

        return $num;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-21 17:54
     * @功能说明:获取当前观看视频的id
     */
    public function nowVideo($class_id,$user_id){

        $dis = [

            'a.class_id' => $class_id,

            'a.user_id'  => $user_id,

            'b.status' => 1
        ];

        $video_id = $this->alias('a')
                ->join('longbing_card_payclass_video b','a.video_id = b.id')
                ->where($dis)
                ->order('a.create_time desc,a.id desc')
                ->value('a.video_id');
        //没有观看记录就查询课程的第一条
        if(empty($video_id)){

            $dis = [

                'class_id' => $class_id,

                'status'   => 1,
            ];

            $video_id = Db::name('longbing_card_payclass_video')->where($dis)->where('url','<>','')->order('top desc,id desc')->value('id');

        }

        return $video_id;


    }














}